package com.yjkf.zh.admin.util;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjkf.zh.admin.model.aliyunIot.DevicePropertyPost;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Date;


public class PojoUtil {
    /**
     *判断你一个类是否存在某个属性（字段）
     *
     * @param field 字段
     * @param obj   类对象
     * @return true:存在，false:不存在, null:参数不合法
     */
    public static Object isExistField(String field, Object obj) {
        if (obj == null || StringUtils.isEmpty(field)) {
            return null;
        }
        Object o = JSON.toJSON(obj);
        JSONObject jsonObj = new JSONObject();
        if (!(o instanceof JSONObject)) {
            return null;
        }
        jsonObj = (JSONObject) o;
            return jsonObj.get(field);


     //   return jsonObj.get(field);
    }


    public static void main(String[] args) throws Exception {
        //Class clazz = Class.forName("com.yjkf.zh.admin.model.aliyunIot.DevicePropertyPost");
      //  Constructor c = clazz.getConstructor(String.class,int.class);   //获取有参构造
       // Person p = (Person) c.newInstance("张三",23);                     //通过有参构造创建对象

        //Field f = clazz.getField("name");                             //获取姓名字段
        //f.set(p, "李四");                                               //修改姓名的值
      //  Field f = clazz.getDeclaredField("productKey");                       //暴力反射获取字段

        DevicePropertyPost.Channel01 devicePropertyPost = new DevicePropertyPost.Channel01();
        Tds tds = new Tds();
        tds.setTime(new Date());
        tds.setValue(56f);
        devicePropertyPost.setValue(45F);
        System.out.println(PojoUtil.isExistField("value",devicePropertyPost.getValue()));
    }



}
class Tds{
      private   Date time;
private  Float value;

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public Float getValue() {
        return value;
    }

    public void setValue(Float value) {
        this.value = value;
    }
}
